﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OMap.Tools {

    public static class ValidationHelper {

        public static bool IsNullOrEmpty<T>(IList<T> list2test) {
            if (list2test == null || list2test.Count == 0) {
                return true;
            }
            return false;
        }

        public static bool IsNullOrEmpty<T>(ICollection<T> list2test) {
            if (list2test == null || list2test.Count == 0) {
                return true;
            }
            return false;
        }

        public static bool IsNullOrEmpty<T>(IEnumerable<T> list2test) {
            if (list2test == null || list2test.Count<T>() == 0) {
                return true;
            }
            return false;
        }

        public static bool IsNullOrEmpty<T>(T[] array2test) {
            if (array2test == null || array2test.Length == 0) {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Checks if the given string is numeric
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumeric(string str) {
            if (String.IsNullOrEmpty(str))
                return false;
            foreach (char c in str) {
                if (!Char.IsNumber(c)) {
                    return false;
                }
            }
            return true;
        }

        public static bool IsNotNullAndTypeOf<TTargetType>(object objectToCheck, out TTargetType convertedObject) {
            if (objectToCheck != null && objectToCheck is TTargetType) {
                convertedObject = (TTargetType)objectToCheck;
                return true;
            }
            convertedObject = default(TTargetType);
            return false;
        }


        public static bool IsGuid(string expression) {

            Regex GuidRegExWithComma = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$");
            Regex GuidRegExWithoutComma = new Regex(@"^(\{{0,1}([0-9a-fA-F]){8}([0-9a-fA-F]){4}([0-9a-fA-F]){4}([0-9a-fA-F]){4}([0-9a-fA-F]){12}\}{0,1})$");

            if (expression != null) {
                if (GuidRegExWithComma.IsMatch(expression)) {
                    return true;
                }
                if (GuidRegExWithoutComma.IsMatch(expression)) {
                    return true;
                }
            }
            return false;
        }

    }
}


